﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Vintner.Masters.ModuleData;

namespace Vintner.Masters.Forms
{
    public partial class TaxCodes : ApplicationLibrary.DBManager.DocumentForm
    {
        public TaxCodes()
            : base("TaxCodes")
        {
            InitializeComponent();
            keyControl = txtCode;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarTaxCodesParam(txtCode.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerTaxCode("taxForm", new RadarTaxCodes(), this);
            dbManager.AddMaster(this, VI_TaxCode.Name, true);
        }

        protected override void OnPrepareAuxData()
        {
        }

        protected override void OnBindData()
        {
            BindControl(txtCode, VI_TaxCode.Code);
            BindControl(txtDesc, VI_TaxCode.Description);
            BindControl(ntbPerc, VI_TaxCode.Percent);
        }

        protected override bool OnBeforeSave()
        {
            if (txtCode.Text.Equals(string.Empty))
            {
                MyMessageBox(this, Properties.Resources.Msg_TxtTitle,
                Properties.Resources.Error,
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return base.OnBeforeSave();
        }

        protected override void FocusOnNew()
        {
            txtCode.Focus();
        }

        protected override void OnDisableControlsForEdit()
        {
            txtCode.Enabled = false;
        }

        #endregion
    }

    #region DbManager TaxCodes

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerTaxCode : DBManager
    {
        public dbmanagerTaxCode(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        {
        }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new VI_TaxCode();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(VI_TaxCode.Code, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", VI_TaxCode.Code);
            sParam.Value = "";
            PList.Add(sParam);
            return PList;
        }

        protected override SqlABCommand CreateMasterCommand(ref List<SqlABParameter> dParam)
        {
            return base.CreateMasterCommand(ref dParam);
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion
}